package com.jingming.consult.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import cn.jiguang.dy.Protocol.mContext
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.api.BasicCallback
import com.jingming.consult.R
import com.jingming.consult.base.BaseActivity
import com.jingming.consult.eventbus.EventBusKeys
import com.jingming.consult.eventbus.EventBusUtils
import com.jingming.consult.jgchat.HandleResponseCode
import com.hjq.toast.ToastUtils
import kotlinx.android.synthetic.main.activitylogin.*
import java.util.regex.Pattern

class LoginActivity : BaseActivity() {
    companion object {
        fun startLoginActivity(context: Context) {
            val intent = Intent(context, LoginActivity::class.java)
            if (!(context is Activity)) {
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activitylogin)
        initView()
    }

    private fun initView() {
        btn_login.setOnClickListener {
            btnOperation(1)
        }
        btn_regist.setOnClickListener {
            btnOperation(2)
        }

    }

    /**
     * 1 登录 2 注册
     */
    fun btnOperation(flag: Int) {
        //登陆验证
        //登陆验证
        val userId: String = etUsername.text.toString().trim()
        val password: String = etPwd.text.toString().trim()
        if (TextUtils.isEmpty(userId)) {
            ToastUtils.show("用户名不能为空")
            return
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.show("密码不能为空")
            return
        }
        if (userId.length < 4 || userId.length > 128) {
            ToastUtils.show("用户名为4-128位字符")
            return
        }
        if (password.length < 4 || password.length > 128) {
            ToastUtils.show("密码为4-128位字符")
            return
        }
        if (isContainChinese(userId)) {
            ToastUtils.show("用户名不支持中文")
            return
        }
        if (!whatStartWith(userId)) {
            ToastUtils.show("用户名以字母或者数字开头")
            return
        }
        if (!whatContain(userId)) {
            ToastUtils.show("只能含有: 数字 字母 下划线 . - @")
            return
        }
        //登陆
        //登陆
        if (flag == 1) {
            JMessageClient.login(userId, password, object : BasicCallback() {
                override fun gotResult(responseCode: Int, responseMessage: String) {
                    if (responseCode == 0) {
                        val myInfo =
                            JMessageClient.getMyInfo()
                        val avatarFile = myInfo.avatarFile
                        //登陆成功,如果用户有头像就把头像存起来,没有就设置null
                        val username = myInfo.userName
                        val appKey = myInfo.appKey
                        ToastUtils.show("登陆成功")
                        EventBusUtils.build(EventBusKeys.LOGINSUCCESS)
                            .put("username", userId).post()
                        finish()
                    } else {
                        ToastUtils.show("\"登陆失败$responseMessage\"")
                    }
                }
            })
            //注册
        } else {
            JMessageClient.register(userId, password, object : BasicCallback() {
                override fun gotResult(i: Int, s: String) {
                    if (i == 0) {
                        ToastUtils.show("注册成功")
                    } else {
                        HandleResponseCode.onHandle(mContext, i, false)
                    }
                }
            })
        }
    }

    private fun isContainChinese(str: String): Boolean {
        val pattern = Pattern.compile("[\u4e00-\u9fa5]")
        val matcher = pattern.matcher(str)
        return matcher.find()
    }

    private fun whatStartWith(str: String): Boolean {
        val pattern = Pattern.compile("^([A-Za-z]|[0-9])")
        val matcher = pattern.matcher(str)
        return matcher.find()
    }

    private fun whatContain(str: String): Boolean {
        val pattern =
            Pattern.compile("^[0-9a-zA-Z][a-zA-Z0-9_\\-@\\.]{3,127}$")
        val matcher = pattern.matcher(str)
        return matcher.find()
    }
}